package com.greyu.ysj.utils;

import java.io.IOException;
import java.net.URLEncoder;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
 
public class SendPhoneMessage {
	
	/*
	 * 发送国际短信
	 */
	public static int sendInternalMsg(String phone,String code) throws Exception {
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod("http://intlapi.1cloudsp.com/intl/api/v2/send");
        postMethod.getParams().setContentCharset("UTF-8");
        postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());

        String accesskey = "yC06FSanrnz4ZVeV"; //用户开发key
        String accessSecret = "GT5bhjU4PwjQoGRHmH4Hy0T6r2yNFfDm"; //用户开发秘钥

        NameValuePair[] data = {
                new NameValuePair("accesskey", accesskey),
                new NameValuePair("secret", accessSecret),
                new NameValuePair("sign", "1243"),
                new NameValuePair("templateId", "1915"),
                new NameValuePair("mobile", phone),
                new NameValuePair("content", URLEncoder.encode(code, "utf-8"))//（示例模板：{1}您好，您的订单于{2}已通过{3}发货，运单号{4}）
        };
        postMethod.setRequestBody(data);
        postMethod.setRequestHeader("Connection", "close");

        int statusCode = httpClient.executeMethod(postMethod);
        System.out.println("statusCode: " + statusCode + ", body: "
                    + postMethod.getResponseBodyAsString());
        return statusCode;
    }
	
	
	
	/**
     * @param args
     * @throws IOException
     * @throws HttpException
     */
	   public static int sendMsg(String phone,String code) throws HttpException, IOException {
        HttpClient client=new HttpClient();
        PostMethod post=new PostMethod("http://gbk.sms.webchinese.cn");
        //��ͷ�ļ�������ת��
        post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");
        NameValuePair[] data={
                new NameValuePair("Uid","akb_miner"),
                new NameValuePair("Key","c42c3c8f7926c14721e7"),
                new NameValuePair("smsMob",phone),
                new NameValuePair("smsText","验证码:"+code)       
        };
        post.setRequestBody(data);
        client.executeMethod(post);
        Header[] headers=post.getRequestHeaders();
        int statusCode=post.getStatusCode();
        System.out.println("statusCode:"+statusCode);
        for(Header h:headers){
            System.out.println(h.toString());
        }
        String result=new String(post.getResponseBodyAsString().getBytes("gbk"));
        System.out.println(result);
        post.releaseConnection();
        return statusCode;
    }
                 
/**
* @Title: getErrorMsg
* @Description: TODO(�����쳣ԭ��)
* @param: @param errorCode
*/
public String getErrorMsg(int errorCode){
	if(errorCode==-1){
		return "û�и��û��˻�";
	}else if(errorCode==-2){
		return "�ӿ���Կ����ȷ";
	}else if(errorCode==-3){
		return "������������";
	}else if(errorCode==-4){
		return "�ֻ��Ÿ�ʽ����ȷ";
	}else if(errorCode==-21){
		return "MD5�ӿ���Կ���ܲ���ȷ";
	}else if(errorCode==-11){
		return "���û�������";
	}else if(errorCode==-14){
		return "�������ݳ��ַǷ��ַ�";
	}else if(errorCode==-41){
		return "�ֻ�����Ϊ��";
	}else if(errorCode==-42){
		return "��������Ϊ��";
	}else if(errorCode==-51){
		return "����ǩ����ʽ����ȷ";
	}else if(errorCode==-6){
		return "IP����";
	}else{
		return "δ֪������:"+errorCode;
	}
	}

public String getInternalErrorMsg(int errorCode,int lang){
	String msg="";
	switch(errorCode) {
	case 9001:
		msg= "签名格式不正确";
		break;
	case 9002:
		msg=  "参数未赋值";
		break;
	case 9003:
		msg=  "手机号码格式不正确";
		break;
	case 9006:
		msg=  "用户accessKey不正确";
		break;
	case 9007:
		msg=  "IP白名单限制";
		break;
	case 9009:
		msg=  "短信内容参数不正确";
		break;
	case 9010:
		msg=  "用户短信余额不足";
		break;
	case 9011:
		msg=  "用户帐户异常";
		break;
	case 9012:
		msg=  "手机号码格式不正确日期时间格式不正确";
		break;
	case 9014:
		msg=  "超出了最大手机号数量";
		break;
	case 9015:
		msg=  "不支持的国家短信";
		break;
	case 9016:
		msg=  "无效的签名或者签名ID";
		break;
	case 9024:
		msg=  "手机号码超频";
		break;		
	default:
		msg = "未知错误码"+errorCode;
		break;
	}
	return msg;
}

}